﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="time_worked.aspx.cs" Inherits="SQT.tracnghiem.Ajax.ASP.Staff.time_worked" %>

<%@ Import Namespace="DataAccess" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<body>
    <%
        int id = int.Parse(Request.QueryString["id"]);
        int month = int.Parse(Request.QueryString["month"]);
        int year = int.Parse(Request.QueryString["year"]);

        int hour = 0, minute = 0;

        foreach (TblCheckManager chk in new TblCheckManagerController().FetchAll().Where(x => x.ManagerId == id && DateTime.Parse(x.CheckIn.ToString()).Month == month && DateTime.Parse(x.CheckIn.ToString()).Year == year && x.Accept == true))
        {
            if (chk.CheckOut != null)
            {
                if (DateTime.Parse(chk.CheckOut.ToString()).Hour >= DateTime.Parse(chk.CheckIn.ToString()).Hour)
                {
                    hour += DateTime.Parse(chk.CheckOut.ToString()).Hour - DateTime.Parse(chk.CheckIn.ToString()).Hour;
                }
                else
                {
                    hour += 24 - DateTime.Parse(chk.CheckIn.ToString()).Hour + DateTime.Parse(chk.CheckOut.ToString()).Hour;
                }

                if (DateTime.Parse(chk.CheckIn.ToString()).Minute <= DateTime.Parse(chk.CheckOut.ToString()).Minute)
                {
                    minute += DateTime.Parse(chk.CheckOut.ToString()).Minute - DateTime.Parse(chk.CheckIn.ToString()).Minute;
                }
                else
                {
                    minute += 60 - DateTime.Parse(chk.CheckIn.ToString()).Minute + DateTime.Parse(chk.CheckOut.ToString()).Minute;
                    hour--;
                }
            }
        }
        if (minute >= 60)
        {
            int temp = minute / 60;
            hour += temp;
            minute = minute - (temp * 60);
        }
        
    %>
    <table class="w3-table w3-striped w3-white w3-bordered">
        <thead>
            <tr class="w3-teal">
                <th>Ngày làm</th>
                <th>Chi tiết giờ làm</th>
                <th>Tổng số giờ</th>
                <th>Xác nhận</th>
            </tr>
        </thead>
        <tr>
            <td>Tháng <%=month %> Năm <%=year %></td>
            <td>&nbsp;</td>
            <td><%=hour %> giờ <%=minute %> phút</td>
            <td>&nbsp;</td>
        </tr>

        <%
            var get_check_manager = new TblCheckManagerController().FetchAll().Where(x => x.ManagerId == id && DateTime.Parse(x.CheckIn.ToString()).Month == month && DateTime.Parse(x.CheckIn.ToString()).Year == year && x.CheckOut != null);
            foreach (TblCheckManager chk in get_check_manager)
            {
                string chk_in = "";
                string chk_out = "";
                string date = "";
                DateTime chk_in_date = DateTime.Parse(chk.CheckIn.ToString());
                if (chk_in_date.Day < 10)
                    date = "0" + chk_in_date.Day.ToString() + "/";
                else
                    date = chk_in_date.Day.ToString() + "/";

                if (chk_in_date.Month < 10)
                    date += "0" + chk_in_date.Month.ToString() + "/";
                else
                    date += chk_in_date.Month.ToString() + "/";

                date += chk_in_date.Year.ToString();

                if (DateTime.Parse(chk.CheckIn.ToString()).Hour < 10)
                    chk_in = "0" + DateTime.Parse(chk.CheckIn.ToString()).Hour.ToString() + ":";
                else
                    chk_in = DateTime.Parse(chk.CheckIn.ToString()).Hour.ToString() + ":";

                if (DateTime.Parse(chk.CheckIn.ToString()).Minute < 10)
                    chk_in += "0" + DateTime.Parse(chk.CheckIn.ToString()).Minute.ToString();
                else
                    chk_in += DateTime.Parse(chk.CheckIn.ToString()).Minute.ToString();

                if (DateTime.Parse(chk.CheckOut.ToString()).Hour < 10)
                    chk_out = "0" + DateTime.Parse(chk.CheckOut.ToString()).Hour.ToString() + ":";
                else
                    chk_out = DateTime.Parse(chk.CheckOut.ToString()).Hour.ToString() + ":";

                if (DateTime.Parse(chk.CheckOut.ToString()).Minute < 10)
                    chk_out += "0" + DateTime.Parse(chk.CheckOut.ToString()).Minute.ToString();
                else
                    chk_out += DateTime.Parse(chk.CheckOut.ToString()).Minute.ToString();

                var get_admin = new TblManagerController().FetchAll().Where(x => x.ManagerId == chk.ManagerId).Single();
                string checkout = "";
                hour = 0;
                minute = 0;
                if (chk.CheckOut != null)
                {
                    checkout = DateTime.Parse(chk.CheckOut.ToString()).Hour.ToString() + ":" + DateTime.Parse(chk.CheckOut.ToString()).Minute.ToString();
                    if (DateTime.Parse(chk.CheckOut.ToString()).Hour >= DateTime.Parse(chk.CheckIn.ToString()).Hour)
                    {
                        hour = DateTime.Parse(chk.CheckOut.ToString()).Hour - DateTime.Parse(chk.CheckIn.ToString()).Hour;
                    }
                    else
                    {
                        hour = 24 - DateTime.Parse(chk.CheckIn.ToString()).Hour + DateTime.Parse(chk.CheckOut.ToString()).Hour;
                    }

                    if (DateTime.Parse(chk.CheckIn.ToString()).Minute <= DateTime.Parse(chk.CheckOut.ToString()).Minute)
                    {
                        minute = DateTime.Parse(chk.CheckOut.ToString()).Minute - DateTime.Parse(chk.CheckIn.ToString()).Minute;
                    }
                    else
                    {
                        minute = 60 - DateTime.Parse(chk.CheckIn.ToString()).Minute + DateTime.Parse(chk.CheckOut.ToString()).Minute;
                        hour--;
                    }
                }
        %>
        <tr>
            <td><%=date %></td>
            <td><%=chk_in %> - <%=chk_out %></td>
            <td><%=hour %> giờ <%=minute %> phút</td>
            <%if (chk.Accept == true)
              { %>
            <td><span><i class="fa fa-check w3-text-teal" style="margin-right: 5px; font-size: 19px"></i></span></td>
            <%}
              else
              { %>
            <td>&nbsp;</td>
            <%} %>
        </tr>
        <%} %>
    </table>
</body>
</html>
